<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/guoTuTuMainIcon.png" alt="logo" />
            <div class="title">果图图云图库</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
          class="mainMenu"
        />
      </a-col>
      <!-- 用户信息展示栏 -->
      <a-col>
        <div v-if="loginUserRole === 'admin'">
          <a-tag color="#ffbf00">管理员</a-tag>
        </div>
        <div v-if="loginUserRole === 'vip'">
          <a-tag color="#E4080A" style="opacity: 73%">会员</a-tag>
        </div>
        <div v-if="loginUserRole === 'svip'">
          <a-tag color="#9348A8">超级会员</a-tag>
        </div>
      </a-col>
      <a-col class="user-login-status">
        <div>
          <div v-if="loginUser.id">
            <a-dropdown>
              <a-space>
                <a-avatar :src="loginUserStore.loginUser.userAvatar" />
                {{ loginUserStore.loginUser.userName ?? '无名' }}
              </a-space>
              <template #overlay>
                <a-menu>
                  <a-menu-item>
                    <a-button type="primary">个人中心</a-button>
                  </a-menu-item>
                  <a-menu-item @click="toEditMyselfPage">
                    <a-button type="primary">编辑资料</a-button>
                  </a-menu-item>
                  <a-menu-item @click="doLogout">
                    <a-button type="primary" danger class="user-logout">退出登录</a-button>
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">去登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>

<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import {
  HomeOutlined,
  UserSwitchOutlined,
  FileAddOutlined,
  PictureOutlined,
  RobotOutlined,
  CloudServerOutlined,
} from '@ant-design/icons-vue'
// @ts-ignore
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStores.ts'
import { userLogoutUsingPost } from '@/api/userController.ts'

const loginUserStore = useLoginUserStore()
// 使用 computed 属性获取登录用户的信息
const loginUser = computed(() => loginUserStore.loginUser)
// 使用 computed 属性获取登录用户的角色
const loginUserRole = computed(() => loginUserStore.loginUser.userRole)
const router = useRouter()

// 未经过权限过滤的原始菜单
const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '图片库',
    title: '图片库',
  },
  {
    key: '/add_picture',
    icon: () => h(FileAddOutlined),
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: '/admin/userManage',
    icon: () => h(UserSwitchOutlined),
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/admin/pictureManage',
    icon: () => h(PictureOutlined),
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/admin/spaceManage',
    icon: () => h(CloudServerOutlined),
    label: '空间管理',
    title: '空间管理',
  },
  {
    key: 'others',
    icon: () => h(RobotOutlined),
    label: h('a', { href: 'https://gitee.com/jam-altman', target: '_blank' }, '了解作者'),
    title: '了解云图库作者',
  },
]

// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  // 管理员才能看到 /admin 开头的菜单
  return menus?.filter((menu) => {
    // @ts-ignore
    if (menu?.key?.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

// 经过权限过滤的菜单
// @ts-ignore
const items = computed<MenuProps['items']>(() => filterMenus(originItems))

/**
 * 菜单路由跳转
 */
// @ts-ignore
const doMenuClick = ({ key }) => {
  if (key !== 'others') {
    router.push({
      path: key,
    })
  } else {
    router.push({
      path: '/',
    })
  }
}

const current = ref<string[]>([])
router.afterEach((to, from, next) => {
  current.value = [to.path]
})

/**
 * 退出登录
 */
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  if (res.data.code === 0) {
    // 退出登录成功，清空用户登录信息状态
    loginUserStore.loginUser = {
      userName: '未登录',
    }
    message.success('退出登录成功')
    // 跳转登录页
    await router.push('/')
  } else {
    message.error('退出登录失败，' + res.data.message)
  }
}

/**
 * 跳转编辑个人资料页面
 */
const toEditMyselfPage = async () => {
  // todo
}
</script>

<style scoped>
#globalHeader {
  caret-color: transparent;
}

.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 16px;
  margin-left: 10px;
}

.logo {
  height: 48px;
}

.user-logout {
  opacity: 90%;
}

.user-login-status {
  margin-right: 10px;
}
</style>
